import { request } from '../utils/request.js'
import store from '../store'
import { Toast, Jump } from '../utils/tools.js'
import cookie from '@/utils/auth.js'


//发送消息
export async function sendmsg(params) {
	console.log('params', params);
	let msgcontent = JSON.parse(params['msgcontent']);
	console.log('ctime', msgcontent['ctime']);
	const lastMsgTime = cookie.get('lastMsgTime') || 0;
	console.log('lastMsgTime', lastMsgTime, msgcontent['ctime'] - lastMsgTime);
	const second = 60 * 3; // 时间间隔差 s
	if (second < msgcontent['ctime'] - lastMsgTime) {
		console.log(`消息间隔相差${second}s`)
		msgcontent['cctime'] = msgcontent['ctime'];
		params['msgcontent'] = JSON.stringify(msgcontent);
		cookie.set('lastMsgTime', msgcontent['cctime']) // 本地更新lastMsgTime 值
	}
	try {
		let res = await request({
			url: "/msg/sendmsg",
			method: "post",
			params
		});
		return res;
	} catch (e) {
		return false;
	}
}

// 获取 消息内容列表
export async function getmsgcontent(params) {
	try {
		let res = await request({
			url: "/msg/getmsgcontent",
			method: "post",
			params
		});
		return res;
	} catch (e) {
		return false;
	}
}
// 消息 对象列表
export async function getmsgtitle(params) {
	try {
		let res = await request({
			url: "/msg/getmsgtitle",
			method: "post",
			params
		});

		return res;
	} catch (e) {
		return false;
	}
}

// 删除 一个 消息对象
export async function deletemsgtitle(params) {
	try {
		let res = await request({
			url: "/msg/deletemsgtitle",
			method: "post",
			params
		});

		return res;
	} catch (e) {
		return false;
	}
}
// 全部已读
export async function ReadAll(params) {
	try {
		let res = await request({
			url: "/msg/ReadAll",
			method: "post",
			params
		});
		return res;
	} catch (e) {
		return false;
	}
}


// 意见反馈
export async function MsgFeedback_SendMsg(params) {
	try {
		const { success, msg } = await request({
			url: "/msg/MsgFeedback_SendMsg",
			method: "post",
			params
		});
		return { success, msg };
	} catch (e) {
		return false;
	}
}

// 意见反馈 列表
export async function Feedback_GetMsg(params) {
	try {
		const res = await request({
			url: "/msg/Feedback_GetMsg",
			method: "post",
			params
		});
		return res;
	} catch (e) {
		return false;
	}
}